package it.polimi.TravelDream.ejb.searchManager;

import it.polimi.TravelDream.ejb.dtos.ExcursionDTO;
import it.polimi.TravelDream.ejb.dtos.FlightDTO;
import it.polimi.TravelDream.ejb.dtos.PackageDTO;
import it.polimi.TravelDream.ejb.dtos.RoomDTO;
import it.polimi.TravelDream.ejb.entities.BasicPackage;
import it.polimi.TravelDream.ejb.entities.Excursion;
import it.polimi.TravelDream.ejb.entities.Flight;
import it.polimi.TravelDream.ejb.entities.Room;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class BasicPackageManager
 */
@Stateless
public class BasicPackageManager implements BasicPackageMgr {

	@PersistenceContext
    private EntityManager em;
	private String name;
	private String city;
	private Date expirationDate;
	private int travelID;
	private List<BasicPackage> packageList = new ArrayList<BasicPackage>();
   
    public BasicPackageManager() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public void searchByName(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_NAME, BasicPackage.class).setParameter("name", this.name).getResultList();
    }

    @Override
    public void searchByExpirationDate(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_EXPIRATION_DATE, BasicPackage.class).setParameter("expirationDate", this.expirationDate).getResultList();
    }
    
    @Override
    public void searchByCity(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_CITY, BasicPackage.class).setParameter("city", this.city).getResultList();
    }
    
    @Override
    public void searchByExpirationDateAndName(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_EXPIRATION_DATE_AND_NAME, BasicPackage.class).setParameter("expirationDate", this.expirationDate)
    			.setParameter("name", this.name).getResultList();
    }
    
    @Override
    public void searchByExpirationDateAndCity(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_EXPIRATION_DATE_AND_CITY, BasicPackage.class).setParameter("expirationDate", this.expirationDate)
    			.setParameter("city", this.city).getResultList();
    }
    
    @Override
    public void searchByNameAndCity(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_NAME_AND_CITY, BasicPackage.class).setParameter("name", this.name)
    			.setParameter("city", this.city).getResultList();
    }
    
    @Override
    public void searchByNameCityAndExpirationDate(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_NAME_CITY_AND_EXPIRATION_DATE, BasicPackage.class).setParameter("name", this.name)
    			.setParameter("city", this.city).setParameter("expirationDate", this.expirationDate).getResultList();
    }
    

    @Override
    public void searchAllAvailablePackage() {
    	this.packageList = em.createNamedQuery(BasicPackage.FIND_ALL_AVAILABLE_PACKAGE, BasicPackage.class).getResultList();
    }

    @Override
    public void searchByTravelID(){
    	this.packageList=em.createNamedQuery(BasicPackage.FIND_PACKAGES_BY_TRAVELID, BasicPackage.class).setParameter("travelID", this.travelID).getResultList();
    }

    @Override
    public int getTravelID() {
		return travelID;
	}

    @Override
	public void setTravelID(int travelID) {
		this.travelID = travelID;
	}

	@Override
    public List <PackageDTO> getPackageResultList(){
 	   List <PackageDTO> packageDTOs = new ArrayList <PackageDTO>();
    	
	 	   for(int i=0; i<this.packageList.size(); i++){
	 		  PackageDTO packageDTO = convertToDTO (this.packageList.get(i));
	 		  packageDTOs.add(packageDTO);
	 	   }
	 	   return packageDTOs;
    }
 	 
    @Override
    public String getName() {
		return name;
	}

    @Override
	public void setName(String name) {
		this.name = name;
	}

    @Override
	public String getCity() {
		return city;
	}

    @Override
	public void setCity(String city) {
		this.city = city;
	}

    @Override
	public Date getExpirationDate() {
		return expirationDate;
	}

    @Override
	public void setExpirationDate(Date expirationDate) {
		this.expirationDate = expirationDate;
	}

	private PackageDTO convertToDTO (BasicPackage basicPackage){
    	PackageDTO packageDTO = new PackageDTO();
    	packageDTO.setTravelID(basicPackage.getTravelID());
    	packageDTO.setName(basicPackage.getName());
    	packageDTO.setCity(basicPackage.getCity());
    	packageDTO.setDescription(basicPackage.getDescription());
    	packageDTO.setExcursions(convertExcursionList(basicPackage.getExcursions()));
    	packageDTO.setFlights(convertFlightList(basicPackage.getFlights()));
    	packageDTO.setRooms(convertRoomList(basicPackage.getRooms()));
    	packageDTO.setLastMinute(basicPackage.getLastMinute());
    	packageDTO.setExpirationDate(basicPackage.getExpirationDate());
    	packageDTO.setStartingDate(basicPackage.getStartingDate());
    	packageDTO.setNation(basicPackage.getNation());
    	packageDTO.setPlacesAvailable(basicPackage.getPlacesAvailable());
    	return packageDTO;
    }
    
    private FlightDTO convertFlightToDTO (Flight flight){
    	FlightDTO flightDTO = new FlightDTO();
    	flightDTO.setFlightID(flight.getFlightID());
    	flightDTO.setArrivalAirport(flight.getArrivalAirport());
    	flightDTO.setArrivalCity(flight.getArrivalCity());
    	flightDTO.setDepartureAirport(flight.getDepartureAirport());
    	flightDTO.setDepartureCity(flight.getDepartureCity());
    	flightDTO.setDepartureDate(flight.getDepartureDate());
    	flightDTO.setPrice(flight.getPrice());
    	flightDTO.setSeat(flight.getSeat());
    	return flightDTO;
    }
    
    private List<FlightDTO> convertFlightList(List<Flight> flights){
    	
    	List<FlightDTO> fdtos = new ArrayList<FlightDTO>();
    	for(Flight f : flights){
    		fdtos.add(convertFlightToDTO(f));
    	}
    	return fdtos;
    }
    
    private ExcursionDTO convertExcursionToDTO (Excursion excursion){
    	ExcursionDTO excursionDTO = new ExcursionDTO();
    	excursionDTO.setName(excursion.getName());
    	excursionDTO.setDescription(excursion.getDescription());
    	excursionDTO.setNation(excursion.getNation());
    	excursionDTO.setPlacesAvailable(excursion.getPlacesAvailable());
    	excursionDTO.setPrice(excursion.getPrice());
    	
    	return excursionDTO;
    }
    
    private List<ExcursionDTO> convertExcursionList(List<Excursion> excursions){
    	
    	List<ExcursionDTO> edtos = new ArrayList<ExcursionDTO>();
    	for(Excursion e : excursions){
    		edtos.add(convertExcursionToDTO(e));
    	}
    	return edtos;
    }
 
    private RoomDTO convertRoomToDTO(Room room){
    	RoomDTO roomDTO = new RoomDTO();
    	roomDTO.setRoomNumber(room.getRoomNumber());
    	roomDTO.setBeds(room.getBeds());
    	roomDTO.setPrice(room.getPrice());
    	roomDTO.setHotelName(room.getHotelName().getName());
    	return roomDTO;
    }
    
 private List<RoomDTO> convertRoomList(List<Room> rooms){
    	
    	List<RoomDTO> rdtos = new ArrayList<RoomDTO>();
    	for(Room r : rooms){
    		rdtos.add(convertRoomToDTO(r));
    	}
    	return rdtos;
    }
}
